package com.itcast.ssm.controller;


import com.itcast.ssm.common.CommonResult;
import com.itcast.ssm.common.CommonResultCode;
import com.itcast.ssm.pojo.House;
import com.itcast.ssm.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 房屋信息 前端控制器
 * </p>
 *
 * @author 三人行
 * @since 2020-09-12
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    HouseService houseService;

    @PostMapping("/fileupload")
    public CommonResult uploadFile(@RequestParam(required = false, value = "house_id") Integer houseId,
                                   MultipartFile img) {
        String result = houseService.uploadFile(houseId, img);
        if (result.equals("-1")) {
            return CommonResult.CommonFail();
        }
        return new CommonResult(CommonResultCode.CommonSuccess, result);
    }

    @PostMapping(value = "")
    public CommonResult insert(@RequestParam(value = "owner_id") Integer ownerId,
                               @RequestBody House house) {
        house.setHouseOwnerId(ownerId);
        String result = houseService.insert(house);
        if(!Character.isDigit(result.charAt(0))) {
            return new CommonResult(CommonResultCode.CommonFail, result);
        }
        return new CommonResult(CommonResultCode.CommonSuccess, result);
    }

    @GetMapping("/findAll")
    public CommonResult findAll(@RequestParam(value = "user_id") Integer userId) {
        List<House> houses = houseService.findAll();
        if (houses == null) {
            return CommonResult.CommonFail();
        }
        return new CommonResult(CommonResultCode.CommonSuccess, houses);
    }

    @GetMapping("")
    public CommonResult findById(@RequestParam(value = "house_id") Integer houseId) {
        House house = houseService.findById(houseId);
        if (house == null) {
            return CommonResult.CommonFail();
        }
        return new CommonResult(CommonResultCode.CommonSuccess, house);
    }

    @PutMapping("")
    public CommonResult update(@RequestParam(value = "house_id") Integer houseId,
                               @RequestBody House house) {
        house.setHouseId(houseId);
        String result = houseService.update(house);
        if (result.charAt(0) == '-' || result.charAt(0) == '0' || !Character.isDigit(result.charAt(0))) {
            return new CommonResult(CommonResultCode.CommonFail, result);
        }
        return new CommonResult(CommonResultCode.CommonSuccess, result);
    }

    @DeleteMapping("")
    public CommonResult delete(@RequestParam(value = "house_id") Integer houseId) {
        Integer result = houseService.delete(houseId);
        if (result <= 0) {
            return CommonResult.CommonFail();
        }
        return CommonResult.CommonSuccess();
    }
}